<template>
  <div class="page-container">
    <el-container>
      <el-aside width="200px">
        <div class="item">
          <el-select v-model="filters.shopid" placeholder="所属门店" @change="shopChangeProductType()">
            <el-option
              v-for="item in shopIds"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
          <el-tree
            :data="treeData"
            :props="defaultProps"
            node-key="id"
            accordion
            @node-click="handleNodeClick">
          </el-tree>
        </div>
      </el-aside>
      <el-main>
        <!--工具栏-->
        <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
          <el-form :inline="true" :model="filters" :size="size" ref="filters">
            <el-row>
              <el-form-item>
                <kt-button
                  icon="fa fa-plus"
                  :label="$t('action.add')"
                  type="primary"
                  @click="handleAdd"
                />
              </el-form-item>
            </el-row>
          </el-form>
        </div>
        <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
          <!--表格显示列界面-->
          <table-column-filter-dialog
            ref="tableColumnFilterDialog"
            :columns="columns"
            @handleFilterColumns="handleFilterColumns"
          ></table-column-filter-dialog>
        </div>
        <!--表格内容栏-->
        <cy-table
          :height="350"
          :data="pageResult"
          :columns="filterColumns"
          @findPage="findPage"
          @handleEdit="handleEdit"
          @handleDelete="handleDelete"
          ref="CyTable"
        ></cy-table>
      </el-main>
    </el-container>

    <!--新增编辑界面-->
    <el-dialog
      :title="'新增'"
      width="25%"
      :visible.sync="editDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="80px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
      >
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="属性名称" prop="property_id">
          <el-select v-model="dataForm.property_id" filterable placeholder="请选择">
            <el-option
            v-for="item in propertyIds"
            :key="item.id"
            :label="item.name"
            :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属店铺" prop="shop_id">
          <el-select v-model="dataForm.shop_id" filterable placeholder="请选择">
            <el-option
            v-for="item in shopList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称" prop="product_id">
          <el-select v-model="dataForm.product_id" filterable placeholder="请选择">
            <el-option
            v-for="item in productIds"
            :key="item.id"
            :label="item.name"
            :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品类型" prop="typeid">
          <el-select v-model="dataForm.typeid" filterable placeholder="请选择">
            <el-option
            v-for="item in pusersList"
            :key="item.id"
            :label="item.text"
            :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sortno">
          <el-input v-model="dataForm.sortno" auto-complete="off" @input="sortnoNumber" placeholder="请输入排序且为整数"></el-input>
        </el-form-item>
        <el-form-item label="属性值" prop="pvalue">
          <el-input v-model="dataForm.pvalue" auto-complete="off" placeholder="请输入属性值"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CyTable from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
import { format } from "@/utils/datetime";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
export default {
  components: {
    CyTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      size: "small",
      filters: {
        t: "productPropertyShop",
        shopid: "",
        typeid:''
      },
      columns: [
        { prop: "id", label: "序号", minWidth: 50 },
        { prop: "create_time", label: "创建时间", minWidth: 120 },
        { prop: "shopName", label: "所属店铺", minWidth: 100 },
        { prop: "propertName", label: "属性名称", minWidth: 80 },
        { prop: "productName", label: "商品名称", minWidth: 120 },
        { prop: "text", label: "商品类型", minWidth: 100 },
        { prop: "sortno", label: "排序", minWidth: 100 },
        { prop: "pvalue", label: "属性值", minWidth: 100 }
      ],
      propertyIds:[],
      productIds:[],
      pusersList:[],
      shopList: [],
      shopIds: [{ value: "", label: "请选择" }],
      typeid:'',
      filterColumns: [],
      treeData: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},
      editDialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      editLoading: false,
      dataFormRules: {
        property_id: [{ required: true, message: "请选择属性名称", trigger: "blur" }],
        shop_id: [{ required: true, message: "请选择所属店铺", trigger: "blur" }],
        product_id: [{ required: true, message: "请选择商品名称", trigger: "blur" }],
        typeid: [{ required: true, message: "请选择商品类型", trigger: "blur" }],
        sortno: [{ required: true, message: "请输入排序且为整数", trigger: "blur" }],
        pvalue: [{ required: true, message: "请输入属性值", trigger: "blur" }]
      },
      defaultProps: {
        children: 'nodes',
        label: 'text'
      },
      // 新增编辑界面数据
      dataForm: {
        t: "productPropertyShop",
        property_id: "",
        shop_id: "",
        product_id: "",
        typeid: "",
        sortno: "",
        pvalue: "",
        id:''
      }
    };
  },
  methods: {
    // 批量删除
    handleDelete: function(data) {
      if (data != null && data.params != null && data.params.length > 0) {
        let ids = data.params.map(item => item.id).toString();

        var params = {};
        params.t = "sysConfig";
        params.ids = ids;
        var this_ = this;
        this.utils.request.deleteUserInfo(params, function(res) {
          if (res.code == "0000") {
            this_.$message({ message: "操作成功", type: "success" });
            this_.findPage(null);
          } else {
            this_.$message({ message: "操作失败, " + res.msg, type: "error" });
          }
        });
      }
    },
    // 获取分页数据
    findPage: function(data) {
      this.$refs.CyTable.findPage(this.filters);
    },
    // 显示新增界面
    handleAdd: function() {
      this.editDialogVisible = true;
      this.operation = true;
      this.resetForm("dataForm");
      this.dataForm.id = "";
    },
    // 显示编辑界面
    handleEdit: function(params) {
      this.editDialogVisible = true;
      this.operation = false;
      this.$nextTick(function() {
        this.dataForm = Object.assign({}, params.row);
      });
    },
    // 编辑
    submitForm: function() {
        console.log(1);
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm("确认保存吗？", "提示", {}).then(() => {
            console.log(this.dataForm);
            let params = Object.assign({}, this.dataForm);
            var this_ = this;
            params.t = "productPropertyShop";
            this.utils.request.editInfo(params, function(res) {
              if (res.code == "0000") {
                this_.$message({ message: "操作成功", type: "success" });
                this_.editDialogVisible = false;
                this_.findPage(this_.filters);
                this_.$refs["dataForm"].resetFields();
              } else {
                this_.$message({
                  message: "操作失败, " + res.msg,
                  type: "error"
                });
              }
            });
          });
        }
      });
    },
    //点击树状图刷新列表
    handleNodeClick(data) {
        console.log(data);
        this.filters.typeid = data.id;
        this.findPage(this.filters);
    },
    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },

    resetForm(formName) {
      if (this.$refs[formName] != undefined) {
        this.$refs[formName].resetFields();
      }
    },
    sortnoNumber: function() {
        this.dataForm.sortno = this.dataForm.sortno.replace(/[^\d]/g,'');
    },
    //选择商铺值改变时刷新树状结构
    shopChangeProductType: function() {
        console.log(this.filters.shopid);
        var this_ = this;
        this.utils.request.queryAllProductType({'shop_id':this.filters.shopid}, function(data) {
            if (null != data.data) {
                this_.treeData=data.data;
            }else{
                this_.treeData=[];
            }
        });
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      var temp = [];

      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "系统参数下载";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    }
  },
  mounted() {
    this.initColumns();
    var this_ = this;
    this.utils.request.queryUserList({ t: "shop" }, function(data) {
      if (null != data.data) {
        for (var i = 0; i < data.data.length; i++) {
          var obj = {};
          obj.value = data.data[i].id;
          obj.label = data.data[i].name;
          this_.shopIds.push(obj);
        }
        this_.shopList = data.data;
      }
    });
    this.utils.request.queryAllProductType({'shop_id':this.filters.shop_id}, function(data) {
      if (null != data.data) {
          this_.treeData=data.data;
      }
    });
    
    //商品类型下拉数据
    this.utils.request.queryUserList({ t: "productType" }, function(data) {
      if (null != data.data) {
        this_.pusersList=data.data;
      }
    });
    //属性名称下拉数据
    this.utils.request.queryUserList({ t: "productProperty" }, function(data) {
      if (null != data.data) {
        this_.propertyIds=data.data;
      }
    });
    //商品名称下拉数据
    this.utils.request.queryUserList({ t: "product" }, function(data) {
      if (null != data.data) {
        this_.productIds=data.data;
      }
    });
  }
};
</script>

<style scoped>
.item {
  padding-top: 10px; 
  overflow-y: auto!important;
}
</style>